﻿#include "mainwidget.h"
#include "ui_mainwidget.h"

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWidget)
{
    ui->setupUi(this);
    setWindowState(Qt::WindowFullScreen);

    m_videoDevice = new VideoDevice(QSize(1280, 720), this);
    //将显示图像移到(320, 0)位置
    m_videoDevice->showViewfinder(320, 0);
    m_videoDevice->openCamera();

    m_diskThread = new DiskThread(this);
    QObject::connect(m_diskThread, &DiskThread::diskStatusSignal, this, &MainWidget::diskStatusSlot);

    videoThread = new VideoThread(m_videoDevice, this);
    pictureThread = new PictureThread(m_videoDevice, this);

    QObject::connect(m_videoDevice, &VideoDevice::videoFrameSingal, videoThread, &VideoThread::videoFrameSlot);
    QObject::connect(videoThread, &VideoThread::videoTimeSignal, ui->label, &QLabel::setText);
    QObject::connect(m_videoDevice, &VideoDevice::videoFrameSingal, pictureThread, &PictureThread::pictureFrameSlot);
    QObject::connect(pictureThread, &PictureThread::pictureFinishSignal, this, &MainWidget::pictureFinishSlot);
    QObject::connect(videoThread, &VideoThread::videoFinishSignal, this, &MainWidget::videoFinishSlot);

    if (!m_diskThread->isRunning())
        m_diskThread->start();
    if (!videoThread->isRunning())
        videoThread->start();
    if (!pictureThread->isRunning())
        pictureThread->start();
}

MainWidget::~MainWidget()
{
    delete ui;

    if (m_diskThread->isFinished())
        delete m_diskThread;

    videoThread->stop();
    if (videoThread->isFinished())
        delete videoThread;
    pictureThread->stop();
    if (pictureThread->isFinished())
        delete pictureThread;
}


void MainWidget::recordStart()
{
    videoThread->setRecordPath(diskPath);
    videoThread->setRecordStatus(VideoThread::RECORD_START);
    m_videoDevice->setProbeStatus(true);
}

void MainWidget::recordStop()
{
    if (ui->pictureBtn->text() == tr("Picture"))
        m_videoDevice->setProbeStatus(false);
    videoThread->setRecordStatus(VideoThread::RECORD_STOP);
}

void MainWidget::on_videoBtn_clicked()
{
    if (ui->videoBtn->text() == tr("Stop"))
    {
        recordStop();
        ui->videoBtn->setText(tr("Saveing"));
        ui->videoBtn->setEnabled(false);
        ui->paushBtn->setText(tr("Paush"));
        ui->paushBtn->setEnabled(false);

        return ;
    }
    recordStart();
    ui->videoBtn->setText(tr("Stop"));
    ui->paushBtn->setEnabled(true);
}

void MainWidget::on_paushBtn_clicked()
{
    if (ui->paushBtn->text() == tr("Paush"))
    {
        ui->paushBtn->setText(tr("Continue"));
        m_videoDevice->setProbeStatus(false);
        videoThread->setRecordStatus(VideoThread::RECORD_PAUSE);

        return ;
    }
    ui->paushBtn->setText(tr("Paush"));
    m_videoDevice->setProbeStatus(true);
    videoThread->setRecordStatus(VideoThread::RECORDING);
}

void MainWidget::on_pictureBtn_clicked()
{
    pictureThread->setPicturePath(diskPath);
    pictureThread->setPictureStatus(PictureThread::PICTURE_START);
    m_videoDevice->setProbeStatus(true);
    ui->pictureBtn->setText(tr("Taking"));
    ui->pictureBtn->setEnabled(false);
}

void MainWidget::pictureFinishSlot()
{
    ui->pictureBtn->setText(tr("Picture"));
    ui->pictureBtn->setEnabled(true);

    if (ui->videoBtn->text() == tr("Stop"))
        return ;
    m_videoDevice->setProbeStatus(false);
}

void MainWidget::videoFinishSlot()
{
    ui->videoBtn->setText(tr("Record"));
    ui->videoBtn->setEnabled(true);
}

void MainWidget::diskStatusSlot(DiskThread::DiskStatus status, const QString &path)
{
    if (status == DiskThread::UDISK_ADD)
    {
        ui->videoBtn->setEnabled(true);
        ui->pictureBtn->setEnabled(true);

#ifndef DEF_DEBUG_EN
        //拷贝log.txt到U盘
        QFile file("/usr/app/log.txt");
        if (file.exists()){
            file.copy("/usr/app/log.txt", path+"/log.txt");
        }
        file.close();
        sync();
#endif
    }
    else if (status == DiskThread::UDISK_REMOVE)
    {
        ui->videoBtn->setEnabled(false);
        ui->pictureBtn->setEnabled(false);
    }
    diskPath = path;
}

void MainWidget::on_refreshBtn_clicked()
{
    m_videoDevice->openCamera(true);
}
